/**
 * 前台服务类
 * 负责处理用户请求，如查看影片列表、售票、退票等
 */
public class FrontService extends ManagerService {

    public FrontService(CinemaSystem system) {
        super(system);
    }

    // 1. 售票
    public void sellTicket() {
        System.out.println("\n===== 售票流程 =====");
        if (system.getSessions().isEmpty()) {
            System.out.println("暂无排片信息，请联系经理添加！");
            return;
        }

        // 展示可选场次
        System.out.println("当前排片场次：");
        for (Session session : system.getSessions()) {
            System.out.println("影片：" + session.getMovie().getName()
                    + " | 放映厅：" + session.getHall().getName()
                    + " | 票价：" + session.getPrice());
        }

        // 选择场次
        System.out.print("请选择场次序号：");
        int sessionIndex = Main.sc.nextInt() - 1;
        if (sessionIndex < 0 || sessionIndex >= system.getSessions().size()) {
            System.out.println("无效场次选择！");
            return;
        }
        Session selectedSession = system.getSessions().get(sessionIndex);

        // 展示座位布局
        Hall hall = selectedSession.getHall();
        showSeats(hall, sessionIndex);

        // 选座
        Ticket ticket = selectedSeat(hall, sessionIndex);

        // 输出票信息
        showTicket(hall, sessionIndex, ticket);
    }

    // 3. 退票
    public void refundTicket() {
        System.out.println("\n===== 退票流程 =====");
        if (system.getTickets().isEmpty()) {
            System.out.println("暂无已售车票，无法退票！");
            return;
        }

        // 展示可退票列表
        System.out.println("可退票列表：");
        for (Ticket ticket : system.getTickets()) {
            if (!ticket.isRefunded()) {
                System.out.println(". 票号：" + ticket.getId()
                        + " | 影片：" + ticket.getSession().getMovie().getName()
                        + " | 座位：" + ticket.getRow() + "排" + ticket.getSeat() + "座");
            }
        }

        // 选择退票
        System.out.print("请选择退票序号：");
        int ticketIndex = Main.sc.nextInt() - 1;
        if (ticketIndex < 0 || ticketIndex >= system.getTickets().size()) {
            System.out.println("无效票号选择！");
            return;
        }

        Ticket ticket = system.getTickets().get(ticketIndex);
        // 校验是否已退票
        if (ticket.isRefunded()) {
            System.out.println("该票已退票，无需重复操作！");
            return;
        }

        // 执行退票
        ticket.setRefunded(true);
        ticket.getSession().unlockSeat(ticket.getRow(), ticket.getSeat());
        System.out.println("\n退票成功! 座位已解锁, 可重新售卖");
    }

    // 显示座位图
    private void showSeats(Hall hall, int sessionIndex) {
        Session selectedSession = system.getSessions().get(sessionIndex);
        System.out.println("\n【" + hall.getName() + "】座位布局（"
                + hall.getRowCount() + "排 * " + hall.getSeatPerRow() + "座）：");
        for (int r = 0; r < hall.getRowCount(); r++) {
            for (int s = 0; s < hall.getSeatPerRow(); s++) {
                // O=可选，X=已售
                System.out.print(selectedSession.isSeatAvailable(r + 1, s + 1) ? "O " : "X ");
            }
            System.out.println(); // 换行
        }
    }

    // 选择座位
    private Ticket selectedSeat(Hall hall, int sessionIndex) {
        System.out.print("请输入座位(行 列，如 3 5): ");

        int row = Main.sc.nextInt();
        int seat = Main.sc.nextInt();

        Session selectedSession = system.getSessions().get(sessionIndex);
        // 校验座位合法性
        if (row < 1 || row > hall.getRowCount() || seat < 1 || seat > hall.getSeatPerRow()) {
            System.out.println("座位号超出范围！");
            return null;
        }
        if (!selectedSession.isSeatAvailable(row, seat)) {
            System.out.println("该座位已售出，请重新选择！");
            return null;
        }

        // 锁定座位
        selectedSession.lockSeat(row, seat);
        Ticket ticket = new Ticket(selectedSession, row, seat, selectedSession.getPrice());
        system.getTickets().add(ticket);
        return ticket;
    }

    // 输出票的信息
    private void showTicket(Hall hall, int sessionIndex, Ticket ticket) {
        Session selectedSession = system.getSessions().get(sessionIndex);
        System.out.println("\n售票成功！");
        System.out.println("票号：" + ticket.getId());
        System.out.println("影片：" + selectedSession.getMovie().getName());
        System.out.println("放映厅：" + hall.getName());
        System.out.println("座位：" + ticket.getRow() + "排" + ticket.getSeat() + "座");
        System.out.println("票价：" + ticket.getPrice() + "元");
    }

    @Override
    public void showMenu() {
        // TODO Auto-generated method stub
    }

    @Override
    public void add() {
        // TODO Auto-generated method stub
    }

    @Override
    public void showList() {
        // TODO Auto-generated method stub
    }
}
